﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace UbuntuMigration
{
    public partial class AccountListView : UserControl
    {
        public AccountListView()
        {
            InitializeComponent();
        }

        private void AccountListView_Load(object sender, EventArgs e)
        {
            CheckBox chk = new CheckBox();
            chk.Height = 13;
            chk.DrawToBitmap(checkoff, new Rectangle(0, 0, 13, 13));
            chk.Checked = true;
            chk.DrawToBitmap(checkon, new Rectangle(0, 0, 13, 13));
        }
        private Bitmap checkoff = new Bitmap(13, 13);
        private Bitmap checkon = new Bitmap(13, 13);
        private List<User> _users;
        public List<User> Users
        {
            get
            {
                return _users;
            }
            set
            {
                this._users = value;
                this.Invalidate();
            }
        }
        private void AccountListView_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            if (_users != null)
            {
                this.Height = _users.Count * 70;
                int co = 0;
                foreach (User u in this._users)
                {
                    drawUser(u, g, co);
                    co += 70;
                }
            }
        }
        private void drawUser(User u, Graphics g, int yoffset)
        {
            float fyo = (float)yoffset;
            if (u.selected)
            {
                g.DrawImage(checkon, 5f, fyo + (35f - 6f));
            }
            else
            {
                g.DrawImage(checkoff, 5f, fyo + (35f - 6f));
            }
            
            g.DrawImage(u.profileImageData, new Rectangle(35, yoffset + 5,64,64));
            g.DrawString(u.username, new Font("Segoe UI", 14f), new SolidBrush(Color.SteelBlue), 109f, fyo + 5f);
            g.DrawString("Path: " + u.profiledir, new Font("Segoe UI", 10f), new SolidBrush(Color.Black), 110f, fyo + 30f);
            g.DrawString("Locale: " + u.locale, new Font("Segoe UI", 10f), new SolidBrush(Color.Black), 110f, fyo + 48f);
        }

        private void AccountListView_Click(object sender, EventArgs e)
        {
            if (_users != null)
            {
                Point cur = this.PointToClient(Cursor.Position);
                Rectangle clicked = new Rectangle(cur.X, cur.Y, 1, 1);
                this.Height = _users.Count * 70;
                int co = 0;
                foreach (User u in this._users)
                {
                    Rectangle chkbox = new Rectangle(5, co + (35 - 6), 13, 13);
                    if (chkbox.IntersectsWith(clicked))
                    {
                        u.selected = !u.selected;
                        this.Invalidate(chkbox);
                    }
                    co += 70;
                }
            }
        }
    }
}
